import 'package:dart_jts/dart_jts.dart';
import 'package:turf/turf.dart' as turf;

import '../../entity/point_entity.dart';

/// jts 空间计算
class JtsGeometryUtil {
  static final GeometryFactory _geometryFactory =
  GeometryFactory.defaultPrecision();

  /// 获取多个点中心点 一定是首尾相连的
  static turf.Position getGeometryCenter(List<turf.Position> positions) {
    if (positions.length == 3) {
      var positionStart = positions[0];
      var positionEnd = positions[1];
      return turf.Position.named(
          lat: (positionStart.lat + positionEnd.lat) / 2,
          lng: (positionEnd.lng + positionEnd.lng) / 2);
    }
    List<Coordinate> coordinates = [];
    for (var point in positions) {
      coordinates.add(Coordinate(point.lng.toDouble(), point.lat.toDouble()));
    }
    var lineRing = _geometryFactory.createLinearRing(coordinates);
    var centroid = lineRing.getCentroid();
    return turf.Position.named(lat: centroid.getY(), lng: centroid.getX());
  }

  /// 判断点 和 多点 是否在其内部
  static bool isSelect(turf.Position position, List<turf.Position> points) {
    var point = _geometryFactory.createPoint(
        Coordinate(position.lng.toDouble(), position.lat.toDouble()));
    List<Coordinate> coordinates = [];
    for (var point in points) {
      coordinates.add(Coordinate(point.lng.toDouble(), point.lat.toDouble()));
    }
    coordinates.add(coordinates[0]);
    var lineRing = _geometryFactory.createLinearRing(coordinates);
    var polygon = _geometryFactory.createPolygon(lineRing, null);
    return polygon.intersects(point);
  }

  /// 获取四至点
  static List<turf.Position> getGeometryEnvelope(
      List<turf.Position> positions) {
    List<turf.Position> resultPoints = [];
    List<Coordinate> coordinates = [];
    for (var point in positions) {
      coordinates.add(Coordinate(point.lng.toDouble(), point.lat.toDouble()));
    }
    var lineRing = _geometryFactory.createLineString(coordinates);
    var envelope = lineRing.getEnvelope();
    for (var value in (envelope as Polygon).getCoordinates()) {
      resultPoints.add(turf.Position(value.x, value.y));
    }
    return resultPoints;
  }

  /// 获取四至点
  static Envelope getEnvelope(List<turf.Position> positions) {
    List<Coordinate> coordinates = [];
    for (var point in positions) {
      coordinates.add(Coordinate(point.lng.toDouble(), point.lat.toDouble()));
    }
    var lineRing = _geometryFactory.createLineString(coordinates);
    var envelope = lineRing.getEnvelope();
    return envelope.getEnvelopeInternal();
  }

  /// 是否重叠
  static bool isOverlaps(List<PointEntity> positions1,
      List<PointEntity> positions2) {
    List<Coordinate> coordinates1 = [];
    for (var point in positions1) {
      coordinates1.add(Coordinate(point.longitude!, point.latitude!));
    }
    var lineRing1 = _geometryFactory.createLinearRing(coordinates1);
    var polygon1 = _geometryFactory.createPolygon(lineRing1, []);

    List<Coordinate> coordinates2 = [];
    for (var point in positions2) {
      coordinates2.add(Coordinate(point.longitude!, point.latitude!));
    }
    var lineRing2 = _geometryFactory.createLinearRing(coordinates2);
    var polygon2 = _geometryFactory.createPolygon(lineRing2, []);
    return polygon1.overlaps(polygon2);
  }
}
